package com.bicon.botu.mall.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bicon.botu.mall.goods.commons.GoodsConstants;
import com.bicon.botu.mall.goods.entity.TracePlan;
import com.bicon.botu.mall.goods.mapper.TracePlanMapper;
import com.bicon.botu.mall.goods.service.TracePlanService;
import com.bicon.botu.core.base.common.BusinessException;
import com.bicon.botu.core.base.repository.redis.RedisHandler;
import com.bicon.botu.core.base.repository.redis.RedisTemplateUtil;
import com.bicon.botu.mall.goods.dto.TracePlanDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * <p>微量元素定制方案服务
 * </p>
 *
 * @author tao.zeng.
 * @since 2018/3/16.
 */
@Service
public class TracePlanServiceImpl extends ServiceImpl<TracePlanMapper, TracePlan> implements TracePlanService {


    @Autowired
    private RedisHandler redisHandler;


    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public TracePlan addTracePlan(String userId, String planCode, String suitCrowd, String posterPicture) {
        TracePlan tracePlan = new TracePlan();
        tracePlan.preInsert();
        tracePlan.setPlanCode(planCode);
        tracePlan.setUserId(userId);
        tracePlan.setSuitCrowd(suitCrowd);
        tracePlan.setPosterPicture(posterPicture);
        boolean result = tracePlan.insert();

        if (result) {
            RedisTemplateUtil redisTemplate = redisHandler.getLocalRedisTemplate();
            redisTemplate.cacheHash(GoodsConstants.TracePlan.BOTU_GOODS_TRACE_PLAN, "", tracePlan.getPlanCode(), JSON.toJSONString(tracePlan));
        }

        return result ? tracePlan : null;
    }

    @Override
    public List<TracePlanDTO> getTracePlanList() {
        return baseMapper.getTracePlans();
    }
}
